import 'package:cbec_app/components/decoration/DecorationCoupon/big_mode.dart';
import 'package:cbec_app/components/decoration/DecorationCoupon/rect_clipper.dart';
import 'package:cbec_app/components/decoration/DecorationCoupon/rect_with_half_circle_clipper.dart';
import 'package:cbec_app/components/decoration/DecorationCoupon/small_mode.dart';
import 'package:cbec_app/components/decoration/DecorationCoupon/stamp_clipper.dart';
import 'package:cbec_app/entity/home/content/coupon_decoration_entity.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'Entity/coupon_widget_item_entity.dart';
import 'decoration_coupon_ctr.dart';
import 'inward_rounded_clipper.dart';
import 'package:get/get.dart';

class DecorationCoupon extends StatefulWidget {
  final CouponDecorationEntity model;
  const DecorationCoupon({super.key, required this.model});
  @override
  State<DecorationCoupon> createState() => _DecorationCouponState();
}

class _DecorationCouponState extends State<DecorationCoupon> {
  late DecorationCouponCtr controller;

  bool _isVertical() {
    return widget.model.layout == 1;
  }

  @override
  void initState() {
    super.initState();
    String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
    controller = Get.put(DecorationCouponCtr(), tag: timestamp);
    controller.fetchData(widget.model);
  }

  @override
  Widget build(BuildContext context) {
    return Obx(
      () {
        if (controller.couponList.isNotEmpty) {
          if (_isVertical()) {
            return Container(
              margin: const EdgeInsets.symmetric(vertical: 8),
              child: Wrap(
                runSpacing: 8.0,
                children: controller.couponList.map((coupon) {
                  return BigCouponItem(
                    model: widget.model,
                    item: coupon,
                    callback: () {
                      controller.receiveCoupon(coupon.couponId.toString());
                    },
                  );
                }).toList(),
              ),
            );
          } else {
            return Container(
              margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
              height: 95,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: controller.couponList.map((coupon) {
                  return SmallCouponItem(
                    model: widget.model,
                    item: coupon,
                    callback: () {
                      controller.receiveCoupon(coupon.couponId.toString());
                      controller.fetchData(widget.model);
                    },
                  );
                }).toList(),
              ),
            );
          }
        } else {
          return const SizedBox();
        }
      },
    );
  }
}

// 大图模式
class BigCouponItem extends StatefulWidget {
  final VoidCallback? callback;
  final CouponDecorationEntity model; // 样式
  final CouponWidgetItemEntity item; // 数据

  const BigCouponItem({super.key, required this.model, required this.item, this.callback});

  @override
  State<BigCouponItem> createState() => _BigCouponItemState();
}

class _BigCouponItemState extends State<BigCouponItem> {
  // 整体背景颜色
  late Color couponColor;

  // 价格 的颜色
  late Color priceColor;

  // 名称的颜色
  late Color nameColor;

  // 描述 颜色
  late Color secondaryColor;

  // 半圆颜色
  late Color halfCircleColor;

  // 分割线颜色
  late Color lineColor;

  // 领取 颜色
  late Color claimColor;

  // 边框 颜色
  late Color borderColor;

  configColor() {
    switch (widget.model.couponColor) {
      case 1:
        couponColor = Tools.fromHex('#d7573a');
        priceColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d7573a') : Tools.fromHex('#FFFFFF');
        nameColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d7573a') : Tools.fromHex('#ffffff');
        secondaryColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d85b3f') : Tools.fromHex('#f2c8bf');
        halfCircleColor = Tools.fromHex('#FFFFFF');
        lineColor = Tools.fromHex('#FFFFFF');
        claimColor = Tools.fromHex('#FFFFFF');
        borderColor = Tools.fromHex('#FFFFFF');
      case 2:
        couponColor = Tools.fromHex('#fce9bd');
        priceColor = Tools.fromHex('#e79754');
        nameColor = Tools.fromHex('#e79754');
        secondaryColor = Tools.fromHex('#edb17e');
        halfCircleColor = Tools.fromHex('#FFFFFF');
        lineColor = Tools.fromHex('#e79754');
        claimColor = Tools.fromHex('#e79754');
        borderColor = Tools.fromHex('#FFFFFF');
      case 3:
        couponColor = Tools.fromHex('#FFFFFF');
        priceColor = Tools.fromHex('#c6593e');
        nameColor = Tools.fromHex('#353535');
        secondaryColor = Tools.fromHex('#6a6a6a');
        halfCircleColor = Tools.fromHex('#e4e4e4');
        lineColor = Tools.fromHex('#e4e4e4');
        claimColor = Tools.fromHex('#353535');
        borderColor = Tools.fromHex('#e4e4e4');
      case 4:
        couponColor = Tools.fromHex('#393b3e');
        priceColor = widget.model.couponStyle == 3 ? Tools.fromHex('#393b3e') : Tools.fromHex('#FFFFFF');
        nameColor = widget.model.couponStyle == 3 ? Tools.fromHex('#393b3e') : Tools.fromHex('#FFFFFF');
        secondaryColor = widget.model.couponStyle == 3 ? Tools.fromHex('#393b3e') : Tools.fromHex('#a4a5a6');
        halfCircleColor = Tools.fromHex('#FFFFFF');
        lineColor = Tools.fromHex('#FFFFFF');
        claimColor = Tools.fromHex('#FFFFFF');
        borderColor = Tools.fromHex('#FFFFFF');
      case 5:
        couponColor = Tools.fromHex('#e4f3e5');
        priceColor = Tools.fromHex('#66a963');
        nameColor = Tools.fromHex('#66a963');
        secondaryColor = Tools.fromHex('#8fbf8c');
        halfCircleColor = Tools.fromHex('#FFFFFF');
        lineColor = Tools.fromHex('#66a963');
        claimColor = Tools.fromHex('#8fbf8c');
        borderColor = Tools.fromHex('#FFFFFF');
      default:
        couponColor = Tools.fromHex('#d7573a');
        priceColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d7573a') : Tools.fromHex('#FFFFFF');
        nameColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d7573a') : Tools.fromHex('#ffffff');
        secondaryColor = widget.model.couponStyle == 3 ? Tools.fromHex('#393b3e') : Tools.fromHex('#f2c8bf');
        halfCircleColor = Tools.fromHex('#FFFFFF');
        lineColor = Tools.fromHex('#FFFFFF');
        claimColor = Tools.fromHex('#FFFFFF');
        borderColor = Tools.fromHex('#FFFFFF');
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    configColor();
    return bgStyle();
  }

  Widget bgStyle() {
    switch (widget.model.couponStyle) {
      case 1:
        return ClipPath(
          clipper: StampClipper(),
          child: BigMode(
            couponColor: couponColor,
            priceColor: priceColor,
            nameColor: nameColor,
            secondaryColor: secondaryColor,
            halfCircleColor: halfCircleColor,
            lineColor: lineColor,
            claimColor: claimColor,
            text: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(0, widget.item.drop!.length - 1) : '',
            unit: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(widget.item.drop!.length - 1) : '',
            name: widget.item.name ?? '',
            desc: '${widget.item.content ?? ''}, ${widget.item.goodsType ?? ''}',
            showDivideLine: true,
            callback: widget.callback,
          ),
        );

      case 2:
        return Stack(
          children: [
            ClipPath(
              clipper: InwardRoundedClipper(),
              child: BigMode(
                couponColor: couponColor,
                priceColor: priceColor,
                nameColor: nameColor,
                secondaryColor: secondaryColor,
                halfCircleColor: halfCircleColor,
                lineColor: lineColor,
                claimColor: claimColor,
                text: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(0, widget.item.drop!.length - 1) : '',
                unit: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(widget.item.drop!.length - 1) : '',
                name: widget.item.name ?? '',
                desc: '${widget.item.content ?? ''}, ${widget.item.goodsType ?? ''}',
                callback: widget.callback,
              ),
            ),
            Positioned(
              top: 10,
              left: 10,
              right: 10,
              bottom: 10,
              child: TicketWithBorder(borderColor: borderColor),
            ),
          ],
        );
      case 3:
        return ClipPath(
          clipper: RectWithHalfCircleClipper(),
          child: BigMode(
            couponColor: couponColor,
            priceColor: priceColor,
            nameColor: nameColor,
            secondaryColor: secondaryColor,
            halfCircleColor: halfCircleColor,
            lineColor: lineColor,
            claimColor: claimColor,
            text: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(0, widget.item.drop!.length - 1) : '',
            unit: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(widget.item.drop!.length - 1) : '',
            name: widget.item.name ?? '',
            desc: '${widget.item.content ?? ''}, ${widget.item.goodsType ?? ''}',
            haveBg: true,
            callback: widget.callback,
          ),
        );
      case 4:
        // print('是不是这里呢=====');
        return ClipPath(
          clipper: RectClipper(),
          child: BigMode(
            couponColor: couponColor,
            priceColor: priceColor,
            nameColor: nameColor,
            secondaryColor: secondaryColor,
            halfCircleColor: halfCircleColor,
            lineColor: lineColor,
            claimColor: claimColor,
            text: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(0, widget.item.drop!.length - 1) : '',
            unit: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(widget.item.drop!.length - 1) : '',
            name: widget.item.name ?? '',
            desc: '${widget.item.content ?? ''}, ${widget.item.goodsType ?? ''}',
            callback: widget.callback,
          ),
        );
    }

    return const SizedBox();
  }
}

// 小图模式
class SmallCouponItem extends StatefulWidget {
  final VoidCallback? callback;
  final CouponDecorationEntity model; // 样式
  final CouponWidgetItemEntity item; // 数据
  const SmallCouponItem({super.key, required this.model, required this.item, this.callback});

  @override
  State<SmallCouponItem> createState() => _SmallCouponItemState();
}

class _SmallCouponItemState extends State<SmallCouponItem> {
  // 整体背景颜色
  late Color couponColor;
  // text 颜色
  late Color textColor;
  // desc 颜色
  late Color descColor;

  late Color borderColor;

  void configColor() {
    switch (widget.model.couponColor) {
      case 1:
        couponColor = Tools.fromHex('#d7573a');
        textColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d7573a') : Tools.fromHex('#FFFFFF');
        descColor = widget.model.couponStyle == 3 ? Tools.fromHex('#eaaa9b') : Tools.fromHex('#d7d8d8');
        borderColor = Tools.fromHex('#FFFFFF');
        break;
      case 2:
        couponColor = Tools.fromHex('#fce9bd');
        textColor = Tools.fromHex('#e79754');
        descColor = Tools.fromHex('#eba869');
        borderColor = Tools.fromHex('#FFFFFF');
        break;
      case 3:
        couponColor = Tools.fromHex('#ffffff');
        textColor = Tools.fromHex('#c6593e');
        descColor = Tools.fromHex('#333333');
        borderColor = Tools.fromHex('#F3F3F3');

        break;
      case 4:
        couponColor = Tools.fromHex('#393b3e');
        textColor = widget.model.couponStyle == 3 ? Tools.fromHex('#393b3e') : Tools.fromHex('#FFFFFF');
        descColor = widget.model.couponStyle == 3 ? Tools.fromHex('#757779') : Tools.fromHex('#d7d8d8');
        borderColor = Tools.fromHex('#FFFFFF');
        break;
      case 5:
        couponColor = Tools.fromHex('#e4f3e5');
        textColor = Tools.fromHex('#66a963');
        descColor = Tools.fromHex('#80b87d');
        borderColor = Tools.fromHex('#FFFFFF');
        break;
      default:
        couponColor = Tools.fromHex('#d7573a');
        textColor = widget.model.couponStyle == 3 ? Tools.fromHex('#d7573a') : Tools.fromHex('#FFFFFF');
        descColor = widget.model.couponStyle == 3 ? Tools.fromHex('#eaaa9b') : Tools.fromHex('#d7d8d8');
        borderColor = Tools.fromHex('#FFFFFF');
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    configColor();
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 8),
      width: 140,
      child: bgStyle(),
    );
  }

  Widget bgStyle() {
    final clipper = _getClipper(widget.model.couponStyle ?? 1);
    final childWidget = GestureDetector(
      onTap: widget.callback,
      child: SmallMode(
        text: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(0, widget.item.drop!.length - 1) : '',
        desc: '${widget.item.content ?? ''}, ${widget.item.goodsType ?? ''}',
        unit: widget.item.drop != null && widget.item.drop!.isNotEmpty ? widget.item.drop!.substring(widget.item.drop!.length - 1) : '',
        textColor: textColor,
        descColor: descColor,
      ),
    );

    if (clipper != null) {
      return ClipPath(
        clipper: clipper,
        child: Container(
          color: couponColor,
          child: widget.model.couponStyle == 2
              ? Stack(
                  children: [
                    Positioned(
                      top: 10,
                      left: 10,
                      right: 10,
                      bottom: 10,
                      child: TicketWithBorder(borderColor: borderColor),
                    ),
                    childWidget,
                  ],
                )
              : widget.model.couponStyle == 3
                  ? Stack(
                      children: [
                        Positioned(
                          top: 5,
                          left: 5,
                          right: 15,
                          bottom: 5,
                          child: Container(
                            color: Colors.white,
                          ),
                        ),
                        childWidget,
                      ],
                    )
                  : childWidget,
        ),
      );
    }

    return const SizedBox();
  }

  CustomClipper<Path>? _getClipper(int style) {
    switch (style) {
      case 1:
        return StampClipper();
      case 2:
        return InwardRoundedClipper();
      case 3:
        return RectWithHalfCircleClipper();
      case 4:
        return RectClipper();
      default:
        return null;
    }
  }
}
